layui.define((exports) => {
    "use strict";

    let layer = layui.layer
    let $ = layui.$

    let common = {
        /**
         * 格式化字符串
         * @param {string} format 
         * @param  {...any} args 
         * @returns 
         */
        sprintf: function (format, ...args) {
            let i = 0;
            return format.replace(/%[sdj%]/g, (match) => {
                if (match === '%%') {
                    return '%';
                }
                if (i >= args.length) {
                    return match;
                }
                switch (match) {
                    case '%s':
                        return String(args[i++]);
                    case '%d':
                        return Number(args[i++]);
                    case '%j':
                        return JSON.stringify(args[i++]);
                    default:
                        return match;
                }
            });
        },
        /**
         * 打开firame窗口
         * @param {string} url 
         * @param {string} title 
         */
        open_iframe: function (url, title = "信息") {
            layer.open({
                type: 2,
                area: ['800px', '600px'],
                title: title,
                shade: 0.001, // 遮罩透明度
                shadeClose: true, // 点击遮罩区域，关闭弹层
                maxmin: true, // 允许全屏最小化
                anim: 0, // 0-6 的动画形式，-1 不开启
                content: url,
                btn: ['确定', '重置'],
                yes: function (index, layero, that) {
                    let submit = layer.getChildFrame(`button[type="submit"]`, index)
                    submit.click()
                    return false
                },
                btn2: function (index, layero, that) {
                    let reset = layer.getChildFrame(`button[type="reset"]`, index)
                    reset.click()
                    return false
                }
            });
        },
        /**
         * 打开询问窗口
         * @param {string} url 
         * @param {object} data 
         * @param {string} title 
         * @param {number} icon 
         */
        open_confirm: function (url, data = {}, title = "确定要操作吗？", icon = 2) {
            layer.confirm(title, { icon: icon }, function () {
                let index = layer.load(1)
                $.get(url, data, function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, { icon: 1 }, function () {
                            window.location.reload()
                        })
                    } else {
                        layer.msg(res.msg)
                    }
                    layer.close(index)
                })
            });
        },
        /**
         * 搜索格式化
         * @param {object} data 
         * @returns object
         */
        search_format: function (data) {
            for (let key in data) {
                if (data[key] === "" || data[key] === null || data[key] === undefined) {
                    delete data[key];
                } else {
                    if (data[key].indexOf("~") !== -1) {
                        data[key] = data[key].split("~");
                    }
                }
            }
            return data
        }
    }

    exports('common', common);
})